package com.jiajia.study;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Random;

/**
 * @Title: DLXApplication
 * @Author zjj
 * @Package com.jiajia.study
 * @Date 2024/3/9 15:56
 * @description:
 */

@SpringBootApplication
@Slf4j
public class DLXApplication implements ApplicationRunner {


    @Resource
    private RabbitTemplate rabbitTemplate;

    public static void main(String[] args) {
        SpringApplication.run(DLXApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Thread.sleep(3L);

//        dlxApplyV1();
//        dlxApplyV2();
//        dlxApplyV3();
//        dlxApplyV4();
        dlxApplyV5();

    }
    private void dlxApplyV5() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(new Random().nextInt(5) + 1);
                Message message = new Message(("dlx message v4, queue is ttl " + i).getBytes(StandardCharsets.UTF_8));
                rabbitTemplate.convertAndSend("dlx.exchange.normal.v5","normal",message);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    private void dlxApplyV4() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(new Random().nextInt(5) + 1);
                Message message = new Message(("dlx message v4, queue is ttl " + i).getBytes(StandardCharsets.UTF_8));
                rabbitTemplate.convertAndSend("dlx.exchange.normal.v4","normal",message);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void dlxApplyV3() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(new Random().nextInt(5) + 1);
                Message message = new Message("dlx message v3, queue is ttl ".getBytes(StandardCharsets.UTF_8));
                rabbitTemplate.convertAndSend("dlx.exchange.normal.v3","normal",message);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void dlxApplyV2() {
        Message message = new Message("dlx message v2, queue is ttl ".getBytes(StandardCharsets.UTF_8));
        rabbitTemplate.convertAndSend("dlx.exchange.normal.v2","normal",message);
    }

    private void dlxApplyV1() {
        MessageProperties messageProperties = new MessageProperties();
        // 消息过期时间是 20秒
        messageProperties.setExpiration("20000");
        Message message = new Message("dlx message v1, message is ttl ".getBytes(StandardCharsets.UTF_8),messageProperties);
        rabbitTemplate.convertAndSend("dlx.exchange.normal.v1","normal",message);
    }
}
